<?php

namespace App\Http\Controllers\Admin\Author;
use App\Http\Requests\ArticlesPostRequest;
use Illuminate\Http\Request;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class ArticleController extends Controller
{
    public function getAdd($id)
    {
    	$books = DB::table('hs_books')->where(['aid'=>$id,'status'=>1])->get();

    	return view('admin.author.article.add',['books'=>$books]);
    	// return '文章添加';
    }


    public function postInsert(ArticlesPostRequest $request)
    {
    	// return '文章插入';
    	$data['bid'] = $request->input('bid');
    	$data['title']  = $request->input('title');
    	$data['content']  = $request->input('content');
    	$data['isfree'] = $request->input('isfree');
    	$data['time'] = time();
    	$data['status'] = 0;
    	$data['aid'] = $request->session()->get('id');
    	$res = DB::table('hs_articles')->insert($data);
    	if($res){
    		return redirect('/admin/article/index')->with('success','发布文章成功,等待审核');
    	}else{
    		return back()->with('error','发布文章失败');
    	}
    }

    public function getIndexd(Request $request)
    {
        $id = $request->session()->get('id');
    	$books = DB::table('hs_books')->where('aid','=',2)->get();
    	// dd($books);
    	if($bid = $request->input('sid')){
    		// dd($request->input('id'));
    		$articles = DB::table('hs_articles')->join('hs_books','hs_books.id','=','hs_articles.bid')->where(['hs_articles.aid'=>$id,'hs_articles.bid'=>$bid,'hs_articles.status'=>0])->select('hs_articles.*','hs_books.b_name')->paginate(4);	
    		return view('admin.author.article.indexd',['articles'=>$articles,'books'=>$books,'sid'=>$bid]);
    	}else{
    		$articles = DB::table('hs_articles')->join('hs_books','hs_books.id','=','hs_articles.bid')->where(['hs_articles.aid'=>$id,'hs_articles.status'=>0])->select('hs_articles.*','hs_books.b_name')->paginate(4);
    		return view('admin.author.article.indexd',['articles'=>$articles,'books'=>$books,'sid'=>'']);
    	}
    	// dd($articles);
    	
    }

     public function getIndexh(Request $request)
    {
        $id = $request->session()->get('id');
        $books = DB::table('hs_books')->where('aid','=',$id)->get();
        // dd($books);
        if($bid = $request->input('sid')){
            // dd($request->input('id'));
            $articles = DB::table('hs_articles')->join('hs_books','hs_books.id','=','hs_articles.bid')->where(['hs_articles.aid'=>$id,'hs_articles.bid'=>$bid,'hs_articles.status'=>1])->select('hs_articles.*','hs_books.b_name')->paginate(4);  
            return view('admin.author.article.indexh',['articles'=>$articles,'books'=>$books,'sid'=>$bid]);
        }else{
            $articles = DB::table('hs_articles')->join('hs_books','hs_books.id','=','hs_articles.bid')->where(['hs_articles.aid'=>$id,'hs_articles.status'=>1])->select('hs_articles.*','hs_books.b_name')->paginate(4);
            return view('admin.author.article.indexh',['articles'=>$articles,'books'=>$books,'sid'=>'']);
        }
        // dd($articles);
        
    }

     public function getIndexs(Request $request)
    {
        $id = $request->session()->get('id');
        $books = DB::table('hs_books')->where('aid','=',$id)->get();
        // dd($books);
        if($bid = $request->input('sid')){
            // dd($request->input('id'));
            $articles = DB::table('hs_articles')->join('hs_books','hs_books.id','=','hs_articles.bid')->where(['hs_articles.aid'=>$id,'hs_articles.bid'=>$bid,'hs_articles.status'=>2])->select('hs_articles.*','hs_books.b_name')->paginate(4);  
            return view('admin.author.article.indexs',['articles'=>$articles,'books'=>$books,'sid'=>$bid]);
        }else{
            $articles = DB::table('hs_articles')->join('hs_books','hs_books.id','=','hs_articles.bid')->where(['hs_articles.aid'=>$id,'hs_articles.status'=>2])->select('hs_articles.*','hs_books.b_name')->paginate(4);
            return view('admin.author.article.indexs',['articles'=>$articles,'books'=>$books,'sid'=>'']);
        }
        // dd($articles);
        
    }

    public function getEdit($id)
    {
    	$article = DB::table('hs_articles')->where('id',$id)->first();
    	return view('admin.author.article.edit',['article'=>$article]);
    }

    public function postUpdate(Request $request)
    {
    	$data = $request->except('_token');
    	$data['status'] = 0;
    	// dd($data);
    	$res = DB::table('hs_articles')->where('id',$data['id'])->update($data);
    	if($res){
    		return redirect('/admin/article/indexd')->with('success','更新成功');
    	}else{
    		return redirect('/admin/article/indexs')->with('error','更新失败');
    	}
    }
}
